package ru.ifmo.mlarinsky.classificator.parsing;

import ru.ifmo.mlarinsky.classificator.model.Quote;
import ru.ifmo.mlarinsky.classificator.model.QuoteDate;


/**
 *
 * @author Михаил
 */
public class Parser {
    public static Quote parseDataString(String symbol, String dataString) {
        if (symbol == null) {
            throw new ParseException(dataString, "Symbol is null.");
        }

        if (dataString == null) {
            throw new ParseException(dataString, "Data string is null.");
        }

        String[] patterns = dataString.split(",");
        if (patterns.length != 6) {
            throw new ParseException(dataString, "Wrong patterns number.",
                    patterns);
        }

        String dateString = patterns[0];
        QuoteDate date = parseDate(dateString);
        double open = Double.valueOf(patterns[1]);
        double high = Double.valueOf(patterns[2]);
        double low = Double.valueOf(patterns[3]);
        double close = Double.valueOf(patterns[4]);
        int volume = Integer.valueOf(patterns[5]);

        return new Quote(symbol, date, open, high, low, close, volume);
    }

    private static QuoteDate parseDate(String dateString) {
        try {
            return QuoteDate.valueOf(dateString);
        } catch (Exception e) {
            throw new ParseException(dateString, e.getMessage());
        }
    }
}
